<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2024/4/23
 * Time: 15:19
 */

namespace app\logic\index\income;


use app\logic\BaseLogic;
use app\model\user\UserInviteRelationModel;
use app\model\user\UserModel;
use app\model\user\UserPromotionOrderIncomeModel;
use app\model\user\UserRechargeOrderIncomeModel;

class IncomeFlowLogic extends BaseLogic
{
    protected $model;

    protected static $instance;

    public function __construct()
    {
        parent::__construct();
        $this->model = \app\model\user\UserIncomeFlowModel::getInstance();
    }

    public static function getInstance()
    {
//        if(is_object(self::$instance)){
//            return self::$instance;
//        }

        return  new self();
    }

    public function getTotal($param)
    {
        $data = [];
        //可提现金额
        $data['can_withdraw_amount'] = $this->model->getWhere(['user_sn'=>request()->curUserId,['status','in',[1,2]]])->sum('amount');
        $data['can_withdraw_amount'] = (string) round($data['can_withdraw_amount'],2);

        //今日收益
        $where = ['user_sn'=>request()->curUserId,'type'=>1,['income_date','>=',date('Y-m-d 00"00:00')]];
        $data['today_income_amount'] = $this->model->getWhere($where)->sum('amount');
        $data['today_income_amount'] = (string) round($data['today_income_amount'],2);

        //本月收益
        $where = ['user_sn'=>request()->curUserId,'type'=>1,['income_date','>=',date('Y-m-01 00:00:00')]];
        $data['month_income_amount'] = $this->model->getWhere($where)->sum('amount');
        $data['month_income_amount'] = (string) round($data['month_income_amount'],2);

        //累计收益
        $where = ['user_sn'=>request()->curUserId,'type'=>1];
        $data['total_income_amount'] = $this->model->getWhere($where)->sum('amount');
        $data['total_income_amount'] = (string) round($data['total_income_amount'],2);

        //邀请人数
        $where = ['user_sn'=>request()->curUserId,'level'=>1];
        $data['total_invite_users'] = (string) UserInviteRelationModel::getInstance()->getWhere($where)->count();

        return $data;
    }

    public function getPromotionIncomeList($param)
    {
        $times = [];
        if(isset($param['days'])){
            $times[] = date('Y-m-d',strtotime('-'.$param['days'].' day'));
            if($param['days']==1){
                $times[] = date('Y-m-d 23:59:59',strtotime('- '.$param['days'].' day'));
            }else{
                $times[] = date('Y-m-d 23:59:59',time());
            }
        }elseif(!empty($param['datetime'])){
            $param['datetime'] = explode(',',$param['datetime']);
            $times[] = date('Y-m-d',strtotime($param['datetime'][0]));
            $times[] = date('Y-m-d 23:59:59',strtotime($param['datetime'][1]));
        }

        $where = ['user_sn'=>request()->curUserId];

        if(!empty($times)){
            $where[] = ['pay_time','between',$times];
        }
        if(!empty($param['platform'])){
            $where['platform'] = $param['platform'];
        }
        if(isset($param['level_type'])){
            $where['level_type'] = $param['level_type'];
        }
        if(isset($param['status'])){
            $where['status'] = $param['status'];
        }
        if(!empty($param['keyid'])){
            $where[] = [
                'or'=>[
                    [['videoid','=',$param['keyid']]],
                    [['taskid','=',$param['keyid']]],
                ]
            ];
        }

        $field = ['user_sn','income_amount','pay_amount','official_reward','promotion_reward','pay_time','title','novel_sn',
                  'main_image','business_logo', 'business_name','status','platform','author','anchor_title','refund_amount',
                  'videolink','income_type','videoid','taskid','level_type','agent_level','reward_rate'];
        $data = UserPromotionOrderIncomeModel::getInstance()->index($where,$field,['pay_time','desc']);

        $statusMap = [
            0=>'未结算',
            1=>'已结算',
            2=>'已退款'
        ];
        $levelTypeMap = [
            0=>'达人',
            1=>'直推',
            2=>'间推'
        ];
        $userMap = $this->getListMapById(UserModel::getInstance(),$data['data']->toArray(),'user_sn','user_sn',['field'=>['nickname','user_sn']]);
        foreach($data['data'] as $val){
            $val['status_text'] = $statusMap[$val['status']]??'';
            $val['level_type'] = $levelTypeMap[$val['level_type']]??'';
            $val['nickname'] = isset($userMap[$val['user_sn']])?$userMap[$val['user_sn']]['nickname']:'';

            $val['pay_time'] = date('Y-m-d',strtotime($val['pay_time']));
            $val['pay_amount'] = (string) round($val['pay_amount']-$val['refund_amount'],2);
        }

        return $data;
    }

    public function getRechargeList($param)
    {
        $times = [];
        if(isset($param['days'])){
            $times[] = date('Y-m-d',strtotime('-'.$param['days'].' day'));
            if($param['days']==1){
                $times[] = date('Y-m-d 23:59:59',strtotime('- '.$param['days'].' day'));
            }else{
                $times[] = date('Y-m-d 23:59:59',time());
            }
        }elseif(!empty($param['datetime'])){
            $param['datetime'] = explode(',',$param['datetime']);
            $times[] = date('Y-m-d',strtotime($param['datetime'][0]));
            $times[] = date('Y-m-d 23:59:59',strtotime($param['datetime'][1]));
        }

        $where = ['user_sn'=>request()->curUserId];

        if(!empty($times)){
            $where[] = ['pay_time','between',$times];
        }
        if(isset($param['level_type'])){
            $where['level_type'] = $param['level_type'];
        }
        if(isset($param['status'])){
            $where['status'] = $param['status'];
        }

        $field = ['user_sn','income_source_user_sn','order_no','income_amount','pay_amount','pay_time','status','agent_level','reward_rate','product_type'];
        $data = UserRechargeOrderIncomeModel::getInstance()->index($where,$field,['pay_time','desc']);

        $statusMap = [
            0=>'未结算',
            1=>'已结算',
            2=>'已退款'
        ];

        $userMap = $this->getListMapById(UserModel::getInstance(),$data['data']->toArray(),'income_source_user_sn','user_sn',['field'=>['nickname','headimgurl','user_sn']]);
        foreach($data['data'] as $val){
            $val['status_text'] = $statusMap[$val['status']]??'';
            $val['nickname'] = isset($userMap[$val['income_source_user_sn']])?$userMap[$val['income_source_user_sn']]['nickname']:'';
            $val['headimgurl'] = isset($userMap[$val['income_source_user_sn']])?$userMap[$val['income_source_user_sn']]['headimgurl']:'';

            $val['pay_time'] = date('Y-m-d',strtotime($val['pay_time']));
            $val['pay_amount'] = (string) round($val['pay_amount'],2);
        }

        return $data;
    }


}